package edu.pku.sei.transformation.editor.views.figures;

import java.util.HashMap;

import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;

import edu.pku.sei.transformation.editor.model.requirement.Description;
import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class TransformationGoalFigure extends Ellipse {
	private Label head = new Label("head");
	private Figure body = new Figure();
	private Figure toolTip = new Figure();
	private HashMap<Description, Label> des2labelMap = new HashMap<Description, Label>();
	public TransformationGoalFigure(boolean isOptional) {
		super();
		if (isOptional)
			setLineStyle(SWT.LINE_DASH);
		this.setLayoutManager(new XYLayout());
		head.setFont(FontConstant.ARIAL_9_B);
		head.setIcon(IconsProvider.getImage(IconsProvider.GOAL_ICO));
		this.add(head);
		body.setLayoutManager(new ToolbarLayout(false));
		this.add(body);
		
		this.setToolTip(getToolTipFigure());
	}
	
	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		head.setLocation(rect.getLocation());
		head.setSize(rect.width, rect.height / 2);
		body.setLocation(new Point(rect.x, rect.y + rect.height / 2));
		body.setSize(rect.width, rect.height / 2);
		
	}
	
	public void setName(String s) {
		head.setText(s);
	}
	
	public void addDescrptions(Description des) {
		Label l = new Label(des.getSummary());
		des2labelMap.put(des, l);
		body.add(l);
		this.setToolTip(getToolTipFigure());
	}
	
	public void removeDescription(Description des) {
		Label l = des2labelMap.get(des);
		if (l != null) {
			body.remove(l);
		}
		this.setToolTip(getToolTipFigure());
	}
	
	private Figure getToolTipFigure() {
		toolTip = new Figure();
		toolTip.setLayoutManager(new ToolbarLayout(false));
		toolTip.add(new Label(head.getText()));
		
		for(Description des : des2labelMap.keySet()) {
			this.toolTip.add(new Label(""));
			this.toolTip.add(new Label(des.getSummary()+":"));
			this.toolTip.add(new Label(des.getContent()));
			
		}
		return toolTip;
	}
	
	
	
}
